package cse326.fuellog;

import cse326.fuellog.MainActivity.fillupEntry;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class Fillup
{
	private SQLiteDatabase db;
	private long dbID;
	private long vehicleID;
	private long date;
	private double mileage;
	private double gasPrice;
	private double gallons;
	private double mpg;
	private boolean isPartialFill;

	/**
	 * Creates a fillup object. Uses current date for the date.
	 * 
	 * @param vehicleID
	 *           integer corresponding to vehicle's ID column in database. Vehicle.getID should work.
	 * @param mileage
	 *           miles travelled
	 * @param gasPrice
	 *           the current gas price
	 * @param gallons
	 *           the gallons added
	 */
	public Fillup(int vehicleID,double mileage,double gasPrice,double gallons)
	{
		this.vehicleID = vehicleID;
		this.date = System.currentTimeMillis();
		this.mileage = mileage;
		this.gasPrice = gasPrice;
		this.gallons = gallons;
		this.mpg = this.mileage / this.gallons;
	}

	/**
	 * Creates a new fillup with a bunch of safe (invalid) values.
	 */
	public Fillup()
	{
		this.db = null;
		this.dbID = -1;
		this.vehicleID = -1;
		this.date = -1;
		this.mileage = -1;
		this.gasPrice = -1;
		this.gallons = -1;
		this.mpg = -1;

	}

	/**
	 * @return the dbID
	 */
	public long getDbID()
	{
		return dbID;
	}

	/**
	 * Sets the database ID
	 * 
	 * @param dbID
	 *           database ID
	 */
	public void setDbID(long dbID)
	{
		this.dbID = dbID;
	}

	/**
	 * @return the vehicleID
	 */
	public long getVehicleID()
	{
		return vehicleID;
	}

	/**
	 * @param vehicleID
	 *           the vehicleID to set
	 */
	public void setVehicleID(long vehicleID)
	{
		this.vehicleID = vehicleID;
	}

	/**
	 * @return the date of the fillup
	 */
	public long getDate()
	{
		return date;
	}

	/**
	 * @param date
	 *           the date to set
	 */
	public void setDate(long date)
	{
		this.date = date;
	}

	/**
	 * @return the mileage
	 */
	public double getMileage()
	{
		return mileage;
	}

	/**
	 * @param mileage
	 *           the mileage to set
	 */
	public void setMileage(double mileage)
	{
		this.mileage = mileage;
		updateMPG();
	}

	/**
	 * @return the gasPrice
	 */
	public double getGasPrice()
	{
		return gasPrice;
	}

	/**
	 * @param gasPrice
	 *           the gasPrice to set
	 */
	public void setGasPrice(double gasPrice)
	{
		this.gasPrice = gasPrice;
	}

	/**
	 * @return the gallons added
	 */
	public double getGallons()
	{
		return gallons;
	}

	/**
	 * @param gallons
	 *           the gallons added
	 */
	public void setGallons(double gallons)
	{
		this.gallons = gallons;
		updateMPG();
	}

	/**
	 * Sets the flag if this fillup is a splash-n-go.
	 * 
	 * @param isPartialFill
	 */
	public void setPartialFill(boolean isPartialFill)
	{
		this.isPartialFill = isPartialFill;
	}

	/**
	 * @return if this is a partial fill
	 */
	public boolean isPartialFill()
	{
		return this.isPartialFill;
	}

	/**
	 * gets the cost of the fillup. Gallons * gasPrice rounded to the penny.
	 * 
	 * @return
	 */
	public double getCost()
	{
		return (double)Math.round((this.gallons * this.gasPrice) * 100) / 100;
	}

	/**
	 * Calculates the miles per gallon.
	 */
	public void updateMPG()
	{
		if (this.gallons != 0)
			this.mpg = this.mileage / this.gallons;
		else
			this.mpg = -1;
	}

	/**
	 * Gets the miles per gallon observed in this fillup.
	 * 
	 * @return
	 */
	public double getMPG()
	{
		if (this.mpg == -1)
			this.updateMPG();
		return this.mpg;
	}

	/**
	 * Sets the database pointer
	 * 
	 * @param db
	 *           database this came from
	 */
	public void setDB(SQLiteDatabase db)
	{
		this.db = db;
	}

	/**
	 * Gets the database pointer
	 * 
	 * @return the database this info came from
	 */
	public SQLiteDatabase getDB()
	{
		return this.db;
	}

	/**
	 * Updates this info in the database.
	 */
	public void updateDB()
	{
		if (this.db == null)
		{
			Log.e("Fillup.updateDB","Tried to update, but this.db == null");
			return;
		}

		ContentValues newValues = new ContentValues();
		newValues.put(fillupEntry.COLUMN_NAME_DATE,this.date);
		newValues.put(fillupEntry.COLUMN_NAME_GALLONS,this.gallons);
		newValues.put(fillupEntry.COLUMN_NAME_GASPRICE,this.gasPrice);
		newValues.put(fillupEntry.COLUMN_NAME_MILEAGE,this.mileage);

		String selection = fillupEntry._ID + " LIKE ?";
		String[] selectionArgs = {String.valueOf(this.dbID)};

		this.dbID = db.update(fillupEntry.TABLE_NAME,newValues,selection,selectionArgs);

	}

	/**
	 * Adds this entry to the database. Must have called setDB first.
	 */
	public void addToDB()
	{
		if (this.db == null)
		{
			Log.e("Fillup.addToDB","Tried to add to DB, but this.db == null");
			return;
		}

		ContentValues newValues = new ContentValues();
		newValues.put(fillupEntry.COLUMN_NAME_DATE,this.date);
		newValues.put(fillupEntry.COLUMN_NAME_GALLONS,this.gallons);
		newValues.put(fillupEntry.COLUMN_NAME_GASPRICE,this.gasPrice);
		newValues.put(fillupEntry.COLUMN_NAME_MILEAGE,this.mileage);
		newValues.put(fillupEntry.COLUMN_NAME_VEHICLEID,this.vehicleID);

		this.dbID = db.insert(fillupEntry.TABLE_NAME,null,newValues);

	}
}
